<template>
	<view class="content">
		<view class="row b-b">
			<view class="textop">新手机号:</view>
		    <input class="input" type="number" v-model="phone" placeholder="请输入新手机号" placeholder-class="placeholder" />
		</view>
		<view class="row b-b">
			<view class="textop">验证码:</view>
			<input class="input" type="number" v-model="vcode" maxlength="8" placeholder="请输入手机验证码" placeholder-class="placeholder" />
			<view class="verify-code-selection">
				<text class="btn-get-code" @click="getCode">{{textcode}}</text>
			</view>
		</view>
		<button class="add-btn" @click="confirm">确定</button>
	</view>
</template>

<script>
	import { mapActions } from 'vuex'
	
	export default {
		data() {
			return {
				phone: '',//电话
				vcode : '',// 验证码
				verShow: true,
				textcode: '获取验证码'
			}
		},
		onLoad(option){
	
		},
		methods: {
			getCode(){
					if(this.verShow){
						if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.phone)){
							this.$api.msg('请输入正确的手机号码');
							return;
						}
						uni.request({
							url: this.$common.baseUrl + "/api/user/sendSMS",
							dataType: 'json', //默认 json格式
							data: {
								mobile: this.phone
							},
							method: 'POST', //请求方式
							header: {
								'content-type': 'application/x-www-form-urlencoded', //默认值
							},
							success: (res) => {
								if(res.code == 1){
									uni.setStorageSync('mobile', this.phone)
								}
							},
							fail: (error) => {
								
							}
						})
						  this.verShow = false; 
						  this.textcode = 60;
			              var auth_timer = setInterval(()=>{
			                this.textcode--;
			                if(this.textcode<=0){
			                  this.verShow = true;
							  this.textcode = '获取验证码';
			                  clearInterval(auth_timer);
			                }
			              },1000)	
					}
			},
			//提交
			confirm(){
				if(!this.phone){
					this.$api.msg('请填写新手机号');
					return;
				}
				if(!/(^1[3|4|5|7|8][0-9]{9}$)/.test(this.phone)){
					this.$api.msg('请输入正确的手机号码');
					return;
				}
				if(!this.vcode){
					this.$api.msg('请填写验证码');
					return;
				}
				let obj = {
					phone: this.phone,
					code: this.vcode
				}
				this.$http.post('changephone', obj).then(res => {
					this.$api.msg(res.msg);
					if(res.code == 1){
						setTimeout(() => {
							uni.navigateTo({
								url:'../set/set'
							})
						}, 800)
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.row{
		display: flex;
		justify-content: flex-start;
		align-items: center;
		position: relative;
		padding:0 30upx;
		height: 110upx;
		background: #fff;
		line-height: 110upx;
		.input{
			flex: 1;
			font-size: 30upx;
			color: $font-color-dark;
			margin-left: 40upx;
			margin-top: 6upx;
		}
		.textop{
			font-size: $font-base + 2upx;
		}
	}
	.add-btn{
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		margin: 60upx auto;
		font-size: $font-lg;
		color: #fff;
		background-color: $base-index-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
	.btn-get-code {
		display: flex;
		align-items: center;
		font-size: $font-base - 6upx;
		letter-spacing: 4upx;
		border: 2upx solid #FF4500;
		height: 60upx;
		background: $base-index-color;
		color: #fff;
		border-radius: 10upx;
		padding: 4upx 20upx;
	}
</style>
